package com.test;

import com.dataone.dist.common.JsonUtil;
import com.dataone.dist.goods.model.bean.Category;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @Author: Ant-Man
 * @Date: 2021/2/19 19:39
 */
public class GoodsTwst {

    @Test
    public void test() {
//        long l = RandomUtils.nextLong();
//        System.out.println(l);
//        System.out.println("1234564321".substring(0, 6));

        String j = "[{\"id\":13,\"categoryCode\":\"1013\",\"categoryName\":\"烟类\",\"parentId\":0,\"level\":1,\"status\":1,\"createTime\":1609932392000,\"updateTime\":1612436516000},{\"id\":50,\"categoryCode\":\"10132050\",\"categoryName\":\"烟弹\",\"parentId\":13,\"level\":2,\"status\":1,\"createTime\":1611504332000,\"updateTime\":1612599169000},{\"id\":51,\"categoryCode\":\"101320503051\",\"categoryName\":\"烟弹海外版\",\"parentId\":50,\"level\":3,\"status\":1,\"createTime\":1611504349000,\"updateTime\":1612675277000},{\"id\":74,\"categoryCode\":\"101320503074\",\"categoryName\":\"烟弹国内版\",\"parentId\":50,\"level\":3,\"status\":1,\"createTime\":1612159015000,\"updateTime\":1612436516000},{\"id\":76,\"categoryCode\":\"10132076\",\"categoryName\":\"烟杆\",\"parentId\":13,\"level\":2,\"status\":1,\"createTime\":1612189205000,\"updateTime\":1612436516000},{\"id\":77,\"categoryCode\":\"101320763077\",\"categoryName\":\"烟杆国内版\",\"parentId\":76,\"level\":3,\"status\":1,\"createTime\":1612189251000,\"updateTime\":1612436516000},{\"id\":78,\"categoryCode\":\"101320763078\",\"categoryName\":\"烟杆海外版\",\"parentId\":76,\"level\":3,\"status\":1,\"createTime\":1612189270000,\"updateTime\":1612436516000},{\"id\":80,\"categoryCode\":\"101320503080\",\"categoryName\":\"两颗装烟弹\",\"parentId\":50,\"level\":3,\"status\":1,\"createTime\":1612412518000,\"updateTime\":1612436516000},{\"id\":81,\"categoryCode\":\"101320763081\",\"categoryName\":\"单烟杆\",\"parentId\":76,\"level\":3,\"status\":1,\"createTime\":1612412530000,\"updateTime\":1612436517000},{\"id\":82,\"categoryCode\":\"10132082\",\"categoryName\":\"套装\",\"parentId\":13,\"level\":2,\"status\":1,\"createTime\":1612412542000,\"updateTime\":1612436517000},{\"id\":83,\"categoryCode\":\"101320823083\",\"categoryName\":\"2+6套装\",\"parentId\":82,\"level\":3,\"status\":1,\"createTime\":1612412548000,\"updateTime\":1612436517000},{\"id\":84,\"categoryCode\":\"101320823084\",\"categoryName\":\"1+4套装\",\"parentId\":82,\"level\":3,\"status\":1,\"createTime\":1612412557000,\"updateTime\":1612436516000},{\"id\":86,\"categoryCode\":\"101320823086\",\"categoryName\":\"1+2套装\",\"parentId\":82,\"level\":3,\"status\":1,\"createTime\":1612412589000,\"updateTime\":1612436516000},{\"id\":87,\"categoryCode\":\"10132087\",\"categoryName\":\"一次性电子烟\",\"parentId\":13,\"level\":2,\"status\":1,\"createTime\":1612412619000,\"updateTime\":1612436516000},{\"id\":88,\"categoryCode\":\"101320873088\",\"categoryName\":\"ONE系列\",\"parentId\":87,\"level\":3,\"status\":1,\"createTime\":1612412625000,\"updateTime\":1612675217000},{\"id\":89,\"categoryCode\":\"101320503089\",\"categoryName\":\"单颗装烟弹\",\"parentId\":50,\"level\":3,\"status\":1,\"createTime\":1612412653000,\"updateTime\":1612436516000},{\"id\":127,\"categoryCode\":\"101320503102\",\"categoryName\":\"66\",\"parentId\":50,\"level\":3,\"status\":0,\"createTime\":1612675111000,\"updateTime\":1612675122000},{\"id\":130,\"categoryCode\":\"101320873105\",\"categoryName\":\"555\",\"parentId\":87,\"level\":3,\"status\":1,\"createTime\":1612675532000,\"updateTime\":1612675532000},{\"id\":131,\"categoryCode\":\"101320873106\",\"categoryName\":\"6666666\",\"parentId\":87,\"level\":3,\"status\":0,\"createTime\":1612684560000,\"updateTime\":1612684589000},{\"id\":132,\"categoryCode\":\"100\",\"categoryName\":\"测试\",\"parentId\":0,\"level\":1,\"status\":0,\"createTime\":1613726884000,\"updateTime\":1613726889000}]";
        List<Category> categories = JsonUtil.ofList(j, Category.class);
        List<Category> filter = filter(categories, "烟弹");
        System.out.println(filter);
    }

    private List<Category> filter(List<Category> categories, String suggest) {
        if (StringUtils.isBlank(suggest)) {
            return categories;
        }

        Set<Long> ids = Sets.newHashSet();
        List<Category> fs = Lists.newArrayList();
        categories.stream().filter(predicate(suggest)).forEach(c -> {
            ids.add(c.getId());
            fs.add(c);
        });

        for (Category category : fs) {
            Long parentId = category.getParentId();
            List<Category> parents = parents(categories, parentId);
            for (Category parent : parents) {
                ids.add(parent.getId());
            }
        }
        return categories.stream().filter(c -> ids.contains(c.getId())).collect(Collectors.toList());
    }

    private Predicate<Category> predicate(String suggest) {
        return c -> c.getCategoryCode().contains(suggest) || c.getCategoryName().contains(suggest);
    }

    private List<Category> parents(List<Category> categories, Long parentId) {
        Optional<Category> first = categories.stream().filter(c -> c.getId().equals(parentId)).findFirst();
        List<Category> fCategories = Lists.newArrayList();
        if (first.isPresent()) {
            Category fCategory = first.get();
            fCategories.add(fCategory);
            Long pid = fCategory.getParentId();
            if (Objects.nonNull(pid) && pid > 0) {
                fCategories.addAll(parents(categories, pid));
            }
        }

        return fCategories;
    }
}
